package com.sikaryofficial.backend.aspect;

import com.sikaryofficial.backend.annotation.ReloadActionDataCache;
import com.sikaryofficial.backend.service.useraction.cache.UserActionCacheService;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

import static com.sikaryofficial.backend.constant.ProductConstant.PRODUCT_ACTION_DATA;

/**
 *
 * 功能：用户行为缓存数据过滤拦截切面
 * 时间：2023/11/30 19:15
 * @author qinjinyuan
 */
@Aspect
@Component
@Slf4j
public class ReloadActionDataAspect {
    @Autowired
    private UserActionCacheService userActionCacheService;
    /**
     * 切入点
     */
    @Pointcut("@annotation(com.sikaryofficial.backend.annotation.ReloadActionDataCache)")
    public void userActionCacheMethod() {
    }
    /**
     * 环绕方法
     *
     * @param joinPoint 切点
     * @return 切点
     */
    @Around(value = "userActionCacheMethod()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取方法
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        ReloadActionDataCache reloadActionDataCache = method.getAnnotation(ReloadActionDataCache.class);
        // 加载用户行为数据
        if(reloadActionDataCache.value().equals(PRODUCT_ACTION_DATA)){
            userActionCacheService.loadCurrentUserActionData(SecurityUtils.getUserId());
        }
        return joinPoint.proceed(joinPoint.getArgs());
    }

    @After(value = "userActionCacheMethod()")
    public void after() {

    }
}
